
package net.swa.dewei.service.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import net.swa.dewei.beans.entity.WeixinAddress;
import net.swa.dewei.service.WeixinAddressService;
import net.swa.system.dao.HibernateDaoSupport;

@Service("weixinAddressService")
public class WeixinAddressServiceImpl extends HibernateDaoSupport implements WeixinAddressService
{
    private final Logger log = Logger.getLogger(WeixinAddressServiceImpl.class);

    @SuppressWarnings("unchecked")
    public List<WeixinAddress> queryListByWeixin(String wxid)
    {
        Query q = null;
        Session session = getCurrentSession();
        String hql = "from  WeixinAddress where wxid=:wxid";
        q = session.createQuery(hql);
        q.setString("wxid", wxid);
        List<WeixinAddress> l = q.list();
        return l;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<WeixinAddress> saveReturnList(String wxid , WeixinAddress m)
    {
        List<WeixinAddress> list = null;
        if (!StringUtils.isBlank(wxid))
        {
            Query q = null;
            Session session = getCurrentSession();
            String hql = "update  WeixinAddress  set checked=:checked  where wxid=:wxid";
            q = session.createQuery(hql);
            q.setBoolean("checked", false);
            q.setString("wxid", wxid);
            q.executeUpdate();

            m.setChecked(true);
            if (null == m.getId())
            {
                session.save(m);
            }
            else
            {
                session.update(m);
            }
            hql = " from  WeixinAddress  where wxid=:wxid";
            q = session.createQuery(hql);
            q.setString("wxid", wxid);
            list = q.list();
            log.debug("添加微信地址后返回所有");
        }
        return list;
    }

    @Override
    public WeixinAddress saveReturnModel(String wxid , WeixinAddress m)
    {
        WeixinAddress model = null;
        if (!StringUtils.isBlank(wxid))
        {
            Query q = null;
            Session session = getCurrentSession();
            String hql = "update  WeixinAddress  set checked=:checked  where wxid=:wxid";
            q = session.createQuery(hql);
            q.setBoolean("checked", false);
            q.setString("wxid", wxid);
            q.executeUpdate();

            m.setChecked(true);
            if (null == m.getId())
            {
                session.save(m);
            }
            else
            {
                session.update(m);
            }
            model = m;
        }
        return model;

    }

    @Override
    public WeixinAddress updateReturnModel(String wxid , Long id)
    {
        WeixinAddress model = null;
        if (!StringUtils.isBlank(wxid))
        {
            Query q = null;
            Session session = getCurrentSession();
            String hql = "update  WeixinAddress  set checked=:checked  where wxid=:wxid";
            q = session.createQuery(hql);
            q.setBoolean("checked", false);
            q.setString("wxid", wxid);
            q.executeUpdate();

            model = (WeixinAddress) session.get(WeixinAddress.class, id);
            if (null != model)
            {
                model.setChecked(true);
                session.update(model);
            }
        }
        return model;
    }

    @Override
    public WeixinAddress queryById(Long id)
    {
        Session session = getCurrentSession();
        WeixinAddress r = (WeixinAddress) session.get(WeixinAddress.class, id);
        return r;
    }

    @Override
    public List<WeixinAddress> saveDefault(Long id , String wxid)
    {
        List<WeixinAddress> list = null;
        if (!StringUtils.isBlank(wxid))
        {
            Query q = null;
            String hql = "update  WeixinAddress  set checked=:checked  where wxid=:wxid";
            Session session = getCurrentSession();
            WeixinAddress m = (WeixinAddress) session.get(WeixinAddress.class, id);
            if (null != m && (!m.isChecked()))
            {

                q = session.createQuery(hql);
                q.setBoolean("checked", false);
                q.setString("wxid", wxid);
                q.executeUpdate();

                m.setChecked(true);
                session.update(m);
            }
            hql = " from  WeixinAddress  where wxid=:wxid";
            q = session.createQuery(hql);
            q.setString("wxid", wxid);
            list = q.list();
            log.debug("设置默认地址后返回所有");
        }
        return list;
    }

}